//
//  POSWithNameCell.m
//  NewPro
//
//  Created by 何荧红 on 2019/3/21.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "POSWithNameCell.h"

#import "POSListCell.h"

#import "PostlistModel.h"


@interface POSWithNameCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ViewHeader *secView;

@property (nonatomic, strong) UILabel *conttitle;
@property (nonatomic, strong) UILabel *phonetitle;
@property (nonatomic, strong) UILabel *numbertitle;
@property (nonatomic, strong) UIImageView *arrowimage;

@property (nonatomic, strong) UIView *subView;

@property (nonatomic, assign) BOOL isTimesort;

@property (nonatomic, assign) BOOL isPiliang;

@end

@implementation POSWithNameCell

- (void)setIsPush:(BOOL)isPush{
    _isPush = isPush;
    if (_isPush) {
        self.conttitle.textColor = [UIColor colorWithHexString:@"0CA6F0"];
        self.phonetitle.textColor = [UIColor colorWithHexString:@"0CA6F0"];
        self.numbertitle.textColor = [UIColor colorWithHexString:@"0CA6F0"];
        self.arrowimage.image = [UIImage imageNamed:@"蓝色上拉箭头"];
        self.subView.hidden = NO;
        CGFloat height = 30;
        if (self.dataArr.count > 0) {
            height = 30 + self.dataArr.count * 20;
        }
        [self.subView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height);
        }];
    }else{
        self.conttitle.textColor = [UIColor colorWithHexString:@"333333"];
        self.phonetitle.textColor = [UIColor colorWithHexString:@"333333"];
        self.numbertitle.textColor = [UIColor colorWithHexString:@"333333"];
        self.arrowimage.image = [UIImage imageNamed:@"下拉按钮"];
        self.subView.hidden = YES;
        [self.subView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}
- (void)setContstring:(NSString *)contstring{
    _contstring = contstring;
    if (_contstring) {
        if (_contstring.length > 0) {
            self.conttitle.text = _contstring;
        }
    }
}
- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    if (_dataArr.count > 0) {
        [self.tableView reloadData];
    }
}
- (void)setPhonestring:(NSString *)phonestring{
    _phonestring = phonestring;
    if (_phonestring) {
        if (_phonestring.length > 0) {
            self.phonetitle.text = [NSString stringWithFormat:@"（%@）",[_phonestring stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"]];
        }
    }
}
- (void)setIspiliang:(BOOL)ispiliang{
    _ispiliang = ispiliang;
    self.secView.isPiliang = _ispiliang;
}
- (void)setNumberstr:(NSInteger)numberstr{
    _numberstr = numberstr;
    self.numbertitle.text = [NSString stringWithFormat:@"%ld台",_numberstr];
}
-(void)setCellType:(NSInteger)CellType{
    _CellType = CellType;
    if (self.secView) {
        for (UIView *sub in self.secView.subviews) {
            [sub removeFromSuperview];
        }
        [self.secView CleanPostlistSecView:self.CellType];
        [self.secView.superview layoutIfNeeded];
    }
    if (_CellType == 4) {
        self.phonetitle.hidden = YES;
    }else{
        self.phonetitle.hidden = NO;
    }
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self CleanWithView];
    }
    return self;
}
- (void)CleanWithView{
    WS(weakself);
    self.isPush = NO;
    UIView *Withheader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    Withheader.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    [self addSubview:Withheader];
    self.conttitle = [[UILabel alloc]init];
    self.conttitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:14];
    self.conttitle.textColor = [UIColor colorWithHexString:@"333333"];
    [Withheader addSubview:self.conttitle];
    [self.conttitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(Withheader.mas_centerY);
        make.left.equalTo(Withheader.mas_left).offset(14);
    }];
    self.phonetitle = [[UILabel alloc]init];
    self.phonetitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:10];
    self.phonetitle.textColor = [UIColor colorWithHexString:@"333333"];
    [Withheader addSubview:self.phonetitle];
    [self.phonetitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.conttitle.mas_bottom);
        make.left.equalTo(self.conttitle.mas_right).offset(2);
    }];
    self.arrowimage = [[UIImageView alloc]init];
    self.arrowimage.image = [UIImage imageNamed:@"下拉按钮"];
    [Withheader addSubview:self.arrowimage];
    [self.arrowimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(Withheader.mas_right).offset(-14);
        make.centerY.equalTo(Withheader.mas_centerY);
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(5);
    }];
    
    self.numbertitle = [[UILabel alloc]init];
    self.numbertitle.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    self.numbertitle.textColor = [UIColor colorWithHexString:@"333333"];
    self.numbertitle.text = @"0台";
    [self addSubview:self.numbertitle];
    [self.numbertitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.arrowimage.mas_centerY);
        make.right.equalTo(self.arrowimage.mas_left).offset(-15);
    }];
    
    UIView *line231 = [[UIView alloc]init];
    line231.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
    [Withheader addSubview:line231];
    [line231 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(Withheader.mas_left);
        make.bottom.equalTo(Withheader.mas_bottom);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    UIButton *pushbutton = [[UIButton alloc]init];
    [pushbutton addTarget:self action:@selector(buttonClik) forControlEvents:UIControlEventTouchUpInside];
    [Withheader addSubview:pushbutton];
    [pushbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.subView = [[UIView alloc]init];
    self.subView.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    [self addSubview:self.subView];
    self.subView.hidden = YES;
    [self.subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(Withheader.mas_bottom);
        make.left.mas_equalTo(self.mas_left);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(0);
    }];
    self.secView = [[ViewHeader alloc]initWithPostListHeader];
    [self.secView setTimesortBlock:^(BOOL timesort) {
        weakself.isTimesort = timesort;
    }];
    [self.secView setPiliangBlock:^(BOOL pil) {
        weakself.isPiliang = pil;
    }];
    [self.subView addSubview:_secView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 100) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.tableView registerClass:[POSListCell class] forCellReuseIdentifier:@"POSListCellID"];
    [self.subView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.secView.mas_bottom);
        make.left.equalTo(self.subView.mas_left);
        make.right.equalTo(self.subView.mas_right);
        make.bottom.equalTo(self.subView.mas_bottom);
    }];
}
- (NSMutableArray *)indexlabWith:(UIView *)view{
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (UIView *subv in view.subviews) {
        [arr addObject:subv];
    }
    return arr;
}
- (void)buttonClik{
    if (self.tabPushBlock) self.tabPushBlock(!self.isPush);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    POSListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"POSListCellID"];
    cell.typeV = self.CellType;
    cell.title.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    cell.indexarr = [self indexlabWith:self.secView];
    if (self.CellType == 7 || self.CellType == 8) {
        if (self.dataArr.count > 0) {
            PostCanDetailModel *model = self.dataArr[indexPath.row];
            cell.Dmondel = model;
            [cell setCellClikBlock:^(BOOL push) {
                PostCanDetailModel *model = self.dataArr[indexPath.row];
                model.isClik = push;
                [tableView reloadData];
                if (self.isPiliang) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"POSClikNumber" object:@{[NSString stringWithFormat:@"%ld",self.CellSection]:[NSString stringWithFormat:@"%ld",indexPath.row],@"isPiliang":@"1"}];
                }else{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"POSClikNumber" object:@{[NSString stringWithFormat:@"%ld",self.CellSection]:[NSString stringWithFormat:@"%ld",indexPath.row],@"isPiliang":@"0"}];
                }
            }];
        }
    }else if (self.CellType == 9){
        if (self.dataArr.count > 0) {
            PostCanDetailModel *model = self.dataArr[indexPath.row];
            cell.Dmondel = model;
            [cell setCellClikBlock:^(BOOL push) {
                PostCanDetailModel *model = self.dataArr[indexPath.row];
                model.isClik = push;
                [tableView reloadData];
                if (self.isPiliang) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"HuiBoClikNumber" object:@{[NSString stringWithFormat:@"%ld",self.CellSection]:[NSString stringWithFormat:@"%ld",indexPath.row],@"isPiliang":@"1"}];
                }else{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"HuiBoClikNumber" object:@{[NSString stringWithFormat:@"%ld",self.CellSection]:[NSString stringWithFormat:@"%ld",indexPath.row],@"isPiliang":@"0"}];
                }
            }];
        }
    }else if (self.CellType == 10 || self.CellType == 11){
        if (self.dataArr.count > 0) {
            PostHisDetailModel *model = self.dataArr[indexPath.row];
            cell.HDmodel = model;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
